package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.dto.HomeResponse;
import com.luo.backend.dto.SystemConfigResponse;
import com.luo.backend.dto.UploadResponse;
import com.luo.backend.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统控制器
 */
@RestController
@RequestMapping("/api/v1")
public class SystemController {
    
    private static final Logger logger = LoggerFactory.getLogger(SystemController.class);
    
    @Autowired
    private SystemService systemService;
    
    /**
     * 获取首页数据
     */
    @GetMapping("/home")
    public ApiResponse<HomeResponse> getHomeData() {
        try {
            HomeResponse response = systemService.getHomeData();
            return ApiResponse.success("success", response);
        } catch (Exception e) {
            logger.error("获取首页数据失败", e);
            return ApiResponse.error(500, "获取首页数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取系统配置
     */
    @GetMapping("/system/config")
    public ApiResponse<SystemConfigResponse> getSystemConfig() {
        try {
            SystemConfigResponse response = systemService.getSystemConfig();
            return ApiResponse.success("success", response);
        } catch (Exception e) {
            logger.error("获取系统配置失败", e);
            return ApiResponse.error(500, "获取系统配置失败：" + e.getMessage());
        }
    }
    
    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public ApiResponse<UploadResponse> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            UploadResponse response = systemService.uploadFile(file);
            return ApiResponse.success("文件上传成功", response);
        } catch (Exception e) {
            logger.error("文件上传失败", e);
            return ApiResponse.error(400, "文件上传失败：" + e.getMessage());
        }
    }
}

